<?php
/**
 * 后台附件模型
 * BaseAttachmentModel.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class AdminAttachment extends BaseAttachmentModel
{	
    
    /**
	 * 获取菜单列表
	 *
	 * @param array $params
	 * @return array 数据数组和分页对象
	 */
	public function getList($params){
		$iPage = (isset($params['page']) && !empty($params['page'])) ? $params['page'] : 1;
		$iPagesize = (isset($params['pagesize']) && !empty($params['pagesize'])) ? $params['pagesize'] : Yii::app()->params->pageSize;

		$criteria = new CDbCriteria();
        $criteria->order = 'aid DESC' ;//排序条件 		
		$iCcount = self::model()->count($criteria);
		$oPages = new CPagination($iCcount);
		$oPages->pageSize = $iPagesize;
		$oPages->setCurrentPage($iPage-1);
		$oPages->applyLimit($criteria);
		$oResult = self::model()->findAll($criteria);

		$aList = array();
		if(!empty($oResult)){
			foreach ($oResult as $iKey => $aVal){
				$aList[$aVal->aid]['aid'] = $aVal->aid;
                $aList[$aVal->aid]['uid'] = $aVal->uid;
                $aList[$aVal->aid]['postip'] = $aVal->postip;
                $aList[$aVal->aid]['filename'] = $aVal->filename;
                $aList[$aVal->aid]['size'] = $aVal->size;
                $aList[$aVal->aid]['type'] = $aVal->type;
                $aList[$aVal->aid]['extensionname'] = $aVal->extensionname;
				$aList[$aVal->aid]['filepath'] = $aVal->filepath;
                $aList[$aVal->aid]['serverpath'] = $aVal->serverpath;
                $aList[$aVal->aid]['width'] = $aVal->width;
                $aList[$aVal->aid]['height'] = $aVal->height;
                $aList[$aVal->aid]['modtype'] = $aVal->modtype;
                $aList[$aVal->aid]['isuse'] = $aVal->isuse;
                $aList[$aVal->aid]['dateline'] = $aVal->dateline;
				$alist[$aVal->aid]['location'] = $aVal->location;
			}
		}
		$listResult['list'] = $aList;
		$listResult['pages'] = $oPages;

		return $listResult;
	}
    public static function updateData($attr ,$params){
        if( empty( $attr ) || !is_array($attr) ) return false;
        if( empty( $params ) ) return false;
        $condition = '1';
        if( is_array($params) && $params){
            foreach( $params as $sKey=>$sVal ){
                $condition .= ' AND '.$sKey.'='.$sVal;
            }            
        } else {
            $condition .= ' AND ' .$params;
        }
        //print_r($attr);exit;
        //更新附件的实用状态
        if(self::model()->updateAll($attr ,$condition)){
            return true;
        } else {
            return false;
        }
    }
	/***
	 * 根据aid删除文件
	 */
	public static function deleteAidFile($aid){
		$model = self::model()->findByPk($aid);
		if($model){
			return $model->delete();
		}
        return false;
	}
	//在删除记录后删除文件
	public function afterDelete(){
	    if($this->location=='0'){//本地
	        $dir = @opendir(Yii::app()->params['upload']['uploadlocal'].dirname($this->serverpath));
	        while(($file = readdir($dir))!=false){
	            if(strpos(basename($file),basename($this->serverpath))!==false){
	                @unlink(Yii::app()->params['upload']['uploadlocal'].dirname($this->serverpath).'/'.basename($file));
	            }
	        }
	        closedir($dir);
            return true;
	    }elseif ($this->location=='ftp'){
	        $ftp = Yii::app()->ftp;
	        $dirfile=$ftp->listFiles(dirname($this->serverpath));
	        if(!empty($dirfile)){
	            foreach ($dirfile as $v){
	                if(strpos(basename($v),basename($this->serverpath))!==false){
	                    $ftp->delete(dirname($this->serverpath).'/'.basename($v));
	                }
	            }
	        }
	    }
        return false;
	}
}